home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / varia / gc-nov90.lha / gc-26nov90 / gctest4.C < prev    next >
C/C++ Source or Header  |  1990-11-01  |  2KB  |  89 lines

  1. /* The following program tests the ability of the garbage collector to correct
  2.    derived pointers and pointers in the non-garbage collected heap.
  3. */
  4.  
  5. /* Externals */
  6.  
  7. #include <stdio.h>
  8. #include "gcalloc.h"
  9.  
  10. /* Cell type to build a list with. */
  11.  
  12. struct  cell  {
  13.     cell  *next;
  14.     int* value1;
  15.     int  value2;
  16.     cell( );
  17.     GCCLASS( cell );
  18. };
  19.  
  20. void  cell::GCPointers( )  {
  21.     gcpointer( next );
  22.     gcpointer( value1 );
  23. }
  24.  
  25. cell::cell( )
  26. {
  27.     GCALLOC( cell );
  28.     next = 0;
  29.     value1 = 0;
  30. }
  31.  
  32. typedef  cell* cellptr;
  33.  
  34. /* Dynamic array of cellptr's that is not garbage collected */
  35.  
  36. struct  cella  {
  37.     int  count;
  38.     cellptr ptr[ 1 ];
  39.     cella( int n );
  40. };
  41.  
  42. cella::cella( int n )
  43. {
  44.     this = (cella*)new char*[ (n+1)*4 ];
  45.     count = n;
  46. }
  47.  
  48. gcheap  dummy( 1048576, 2147483647, 1048576, 35, 30, GCMEM+GCSTATS );
  49.  
  50. main()
  51. {
  52.     cella*  pointers = new cella( 50000 );
  53.     for  (int i = 0; i < 50000; i = i+1000)
  54.        gcroots( &pointers->ptr[ i ], 1000*sizeof( int ) );
  55.     cellptr  cl = 0, cp;
  56.  
  57.     /* Allocate 50000 cells referenced by an array in the non-gc heap */
  58.     for  (i = 0; i < 50000; i++)  {
  59.        cp = new cell;
  60.        pointers->ptr[ i ] = cp;
  61.        cp->value1 = 0;
  62.        cp->value2 = i;
  63.     }
  64.  
  65.     /* Make a list of 50000 cells that each point into themseleves. */
  66.     for  (i = 0; i < 50000; i++)  {
  67.        cp = new cell;
  68.        cp = new cell;
  69.        cp = new cell;
  70.        cp = new cell;
  71.        cp->next = cl;
  72.        cp->value1 = &cp->value2;
  73.        cp->value2 = i;
  74.        cl = cp;
  75.     }
  76.  
  77.     /* Verify that objects referenced by pointers still exist */
  78.     for  (i = 0; i < 50000; i++)  {
  79.        if  (pointers->ptr[ i ]->value2 != i)  abort();
  80.     }
  81.  
  82.     /* Verify that cell list is correct correct */
  83.     for  (i = 0; i < 50000; i++)  {
  84.        if  (cl->value2 != *(cl->value1))  abort();
  85.        cl = cl->next;
  86.     }
  87.     exit( 0 );
  88. }
  89.